package com.dense.kuiniu.floatcabinet.mvp.ui.view;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.Group;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ScreenUtils;
import com.dense.kuiniu.floatcabinet.R;
import com.dense.kuiniu.floatcabinet.mvp.http.entity.Book;
import com.dense.kuiniu.floatcabinet.mvp.http.entity.BorrowBook;
import com.dense.kuiniu.floatcabinet.mvp.ui.adapter.BookBackAdapter;
import com.dense.kuiniu.floatcabinet.mvp.ui.adapter.BookBorrowAdapter;

import java.util.List;

/**
 * @author Administrator
 * @date 2020/11/18
 */
public class DialogBorrowResult extends Dialog {
    Group mGroupBorrow;
    Group mGroupReturn;
    TextView mTvBorrowCount;
    TextView mTvReturnCount;
    RecyclerView mRecyclerViewBorrow;
    RecyclerView mRecyclerViewReturn;
    BookBorrowAdapter borrowAdapter;
    BookBackAdapter bookBackAdapter;

    public DialogBorrowResult(@NonNull Context context) {
        super(context);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_borrow_result, null);
        mGroupBorrow = view.findViewById(R.id.group_borrow);
        mGroupReturn = view.findViewById(R.id.group_return);
        mTvBorrowCount = view.findViewById(R.id.tv_borrow_count);
        mTvReturnCount = view.findViewById(R.id.tv_return_count);
        mRecyclerViewBorrow = view.findViewById(R.id.recycler_view_borrow);
        mRecyclerViewReturn = view.findViewById(R.id.recycler_view_return);

        mRecyclerViewBorrow.setLayoutManager(new GridLayoutManager(context, 2));
        mRecyclerViewBorrow.addItemDecoration(new SpaceItemDecoration(20));
        borrowAdapter = new BookBorrowAdapter(R.layout.item_book_borrow);
        mRecyclerViewBorrow.setAdapter(borrowAdapter);

        mRecyclerViewReturn.setLayoutManager(new GridLayoutManager(context, 2));
        mRecyclerViewReturn.addItemDecoration(new SpaceItemDecoration(20));
        bookBackAdapter = new BookBackAdapter(R.layout.item_book_borrow);
        mRecyclerViewReturn.setAdapter(bookBackAdapter);
        setContentView(view);
    }

    public void setBorrowResult(List<BorrowBook> list) {
        mGroupBorrow.setVisibility(View.VISIBLE);
        borrowAdapter.setNewInstance(list);
    }

    public void setBackResult(List<Book> list) {
        mGroupReturn.setVisibility(View.VISIBLE);
        bookBackAdapter.setNewInstance(list);
    }

    @Override
    public void show() {
        super.show();
        measureSize();
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    protected void measureSize() {
        Window dialogWindow = getWindow();
        if (dialogWindow != null) {
            dialogWindow.setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.shape_transparent));
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = (int) (ScreenUtils.getScreenWidth() * 0.8); // 宽度
            lp.height = (int) (ScreenUtils.getScreenHeight() * 0.8); // 高度
            dialogWindow.setGravity(Gravity.CENTER);
            dialogWindow.setAttributes(lp);
            dialogWindow.setWindowAnimations(R.style.DialogOutAndInStyle);
        }
    }
}